package com.ztom.core.io

import java.net.MalformedURLException
import java.net.URL

/**
 * @author ZhangTao
 */
open class DefaultResourceLoader : ResourceLoader {

    private val classpathPrefix = "classpath:"

    override fun getResource(location: String): Resource {
        return if (location.startsWith(classpathPrefix)) {
            ClassPathResource(location.substring(classpathPrefix.length))
        } else {
            try {
                // 先尝试 URL 方式加载
                UrlResource(URL(location))
            } catch (e: MalformedURLException) {
                // 当成文件系统下的资源加载
                FileSystemResource(location)
            }
        }
    }
}